using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Recruiters_contactedresumes : System.Web.UI.Page
{
     
    string connectionString = "";

    int itemPerPage = 10;
    int pagePerSeggment = 5;
     
    void RegisterJavaScript()
    {
        Common common = new Common();
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "openWindow" + this.GridView1.ID, common.CreateScriptForPrint(this.GridView1.ID));
        this.btnPrint.Attributes.Add("onclick", "openWindow" + this.GridView1.ID + "('" + GridView1.ID.ToString() + "',400,700);return false;");

    }
    protected void Page_Load(object sender, EventArgs e)
    {

        if(Session["rcuserid"] == null)
            Response.Redirect("login.aspx");
        connectionString =
         WebConfigurationManager.ConnectionStrings[
           "RecruitVietnam"].ConnectionString;
        RegisterJavaScript();
         
        if (!IsPostBack)
        {
            lblJobID.Text = Request.QueryString.Get("JobID");
            ViewState["SortDirection"] = "ASC";                 
            ShowResult(0);
            
        }
         
        CreateNavigation(Convert.ToInt32(
        literalPages.Text), Convert.ToInt32(
        literalCurrentPage.Text), itemPerPage,
        pagePerSeggment, this.panelNavigation);
         
    }
    void RedefineURL(GridView GridView1)
    {
        HyperLink hyperLink;
        foreach (GridViewRow gridViewRow
        in GridView1.Rows)
        {
            if (gridViewRow.Cells[12].Text == "&nbsp;")
                {
                    if(gridViewRow.Cells[11].Text != "0")
                        gridViewRow.Cells[9].Text = "Job not available";
                    else
                        gridViewRow.Cells[9].Text = "Resume Viewing";
                }
            else
            {
            
                hyperLink = (HyperLink)
                      gridViewRow.Cells[1].Controls[0];
                hyperLink.NavigateUrl += "&JobID=" + gridViewRow.Cells[11].Text;
                
            }
           

        }

        
    } 
    void CreateNavigation(
    int totalPages, int currentPage,
    int itemPerPage, int pagePerSeggent,
    Panel panelLinks)
    {
        panelLinks.Controls.Clear();
        int startPageNo = 0; int endPageNo = 0; int seggmentNo = 0;
        if (totalPages > 1)
        {
            seggmentNo = currentPage / pagePerSeggent;
            if ((currentPage % pagePerSeggent) > 0) seggmentNo += 1;
            startPageNo = pagePerSeggent * (seggmentNo - 1) + 1;
            if (seggmentNo * pagePerSeggent > totalPages)
                endPageNo = totalPages;
            else
                endPageNo = seggmentNo * pagePerSeggent;


            LinkButton linkButton;
            Label labelSpace;

            if (seggmentNo > 1)
            {
                linkButton = new LinkButton();
                linkButton.Text = "First";
                linkButton.Click += new EventHandler(linkButton_Click);
                linkButton.ID = (1).ToString();
                labelSpace = new Label();
                labelSpace.Text = "&nbsp; ";
                panelLinks.Controls.Add(linkButton);
                panelLinks.Controls.Add(labelSpace);
            }

            if (currentPage > pagePerSeggent)
            {
                linkButton = new LinkButton();
                linkButton.Text = "Previous";
                linkButton.Click += new EventHandler(linkButton_Click);
                linkButton.ID = (startPageNo - 1).ToString();
                labelSpace = new Label();
                labelSpace.Text = "&nbsp; ";
                panelLinks.Controls.Add(linkButton);
                panelLinks.Controls.Add(labelSpace);
            }
            int i = 0;
            for (i = startPageNo; i <= endPageNo; i++)
            {
                linkButton = new LinkButton();
                linkButton.Text = i.ToString();

                linkButton.ID = i.ToString();
                if (i == currentPage)
                    linkButton.BackColor = System.Drawing.Color.Aqua;
                linkButton.Click += new EventHandler(linkButton_Click);
                labelSpace = new Label();
                labelSpace.Text = "&nbsp; ";
                panelLinks.Controls.Add(linkButton);
                panelLinks.Controls.Add(labelSpace);
            }
            if (seggmentNo * pagePerSeggent < totalPages)
            {
                linkButton = new LinkButton();
                linkButton.Text = "Next";
                linkButton.ID = (i).ToString();
                linkButton.Click += new EventHandler(linkButton_Click);
                labelSpace = new Label();
                labelSpace.Text = "&nbsp; ";
                panelLinks.Controls.Add(linkButton);
                panelLinks.Controls.Add(labelSpace);
            }
            if (seggmentNo * pagePerSeggent + 1 < totalPages)
            {
                linkButton = new LinkButton();
                linkButton.Text = "Last";
                linkButton.ID = totalPages.ToString();
                linkButton.Click += new EventHandler(linkButton_Click);
                labelSpace = new Label();
                labelSpace.Text = "&nbsp;";
                panelLinks.Controls.Add(linkButton);
                panelLinks.Controls.Add(labelSpace);
            }
        }
    }
    void linkButton_Click(object sender, EventArgs e)
    {

        LinkButton linkButton = (LinkButton)sender;

        int nextPage = 0;
        nextPage = Convert.ToInt32(linkButton.ID);
        literalCurrentPage.Text = nextPage.ToString();
        int endJobs = nextPage * Convert.ToInt32(itemPerPage);
        if (endJobs > Convert.ToInt32(literalTotal.Text)) endJobs = Convert.ToInt32(literalTotal.Text);
        literalRecords.Text = " - Display resume: " + (nextPage - 1) * itemPerPage + " -> " + endJobs.ToString();

        ShowResult(nextPage - 1);
 

    }
    DataTable GetDataTable(int firstRecord)
    {
        SqlServerProvider sqlServerProvider = new SqlServerProvider();
        sqlServerProvider.ConnectionString = connectionString;

        sqlServerProvider.ParameterCollection = new string[3] { "@RecruiterID", "@JobID", "@Total" };
        sqlServerProvider.ValueCollection = new string[3] { Session["rcuserid"].ToString(), lblJobID.Text, "1"};
        sqlServerProvider.CommandText = "SPViewContactByRecruiters";
        sqlServerProvider.CommandType = CommandType.StoredProcedure;
        if (firstRecord == 0)
        {             
            literalTotal.Text = sqlServerProvider.ExecuteScalar().ToString();
            ShowTotalPages(Convert.ToInt32(literalTotal.Text));
        }
        sqlServerProvider.ValueCollection[2] = "0";
        DataTable dataTable = sqlServerProvider.GetDataTable(firstRecord * itemPerPage, itemPerPage);
        return dataTable;
    }
    DataView GetDataView(string columnName, string sortType, int firstRecord)
    {
        SqlServerProvider sqlServerProvider = new SqlServerProvider();
        sqlServerProvider.ConnectionString = connectionString;
        sqlServerProvider.CommandText = "SPViewContactByRecruiters";
        sqlServerProvider.ParameterCollection = new string[3] { "@RecruiterID", "@JobID", "@Total" };
        sqlServerProvider.ValueCollection = new string[3] { Session["rcuserid"].ToString(), lblJobID.Text, "0" };
        sqlServerProvider.CommandType = CommandType.StoredProcedure;
        DataTable dataTable = sqlServerProvider.GetDataTable(firstRecord * itemPerPage, itemPerPage);
        DataView dataView = new DataView(dataTable);
        dataView.Sort = columnName + " " + sortType;
        return dataView;
    }
    void ShowTotalPages(int totalRecords)
    {

        int totalPages = totalRecords / itemPerPage;
        if (totalRecords % itemPerPage != 0)
            totalPages++;
        literalPages.Text = totalPages.ToString();
    }
    void ShowHide(bool flag)
    {
        literalTotal.Visible = flag;
        literalCurrentPage.Visible = flag;
        literalOfText.Visible = flag;
        literalPageText.Visible = flag;
        literalTotalText.Visible = flag;
        literalRecords.Visible = flag;
        literalPages.Visible = flag;
        btnAccept.Visible = flag;
        btnReject.Visible = flag;
        btnDelete.Visible = flag;
        btnPending.Visible = flag;
        btnPrint.Visible = flag;
        
    }
    void ShowResult(int firstRecord)
    {
        try
        {
            GridViewHelper gridViewHelper = new GridViewHelper();
            gridViewHelper.FillDataTableToGridView(
            GetDataTable(firstRecord),
            this.GridView1); ;
            
            if (GridView1.Rows.Count == 0)
            {
                litError.Text = "Resume not found.";
                ShowHide(false);
            }
            else
            {
                ShowHide(true);
                RedefineURL(GridView1);
                litError.Text = "";
                int currentRecords = Convert.ToInt32(literalCurrentPage.Text);
                litError.Text = "";
                literalRecords.Text =
                " - Display resume: " +
                ((currentRecords - 1) * itemPerPage + 1).ToString()
               ;
                if ((currentRecords - 1) * itemPerPage + itemPerPage < Convert.ToInt32(literalTotal.Text))
                    literalRecords.Text +=
                    ((currentRecords - 1) * itemPerPage +
                    itemPerPage).ToString();
                literalRecords.Text += " of " + literalTotal.Text;
            }
            CreateNavigation(Convert.ToInt32(
                       literalPages.Text), Convert.ToInt32(
                       literalCurrentPage.Text), itemPerPage,
                       pagePerSeggment, this.panelNavigation);
        }
        catch (Exception ex)
        {
            litError.Text = ex.Message;
        }
    }
    void ShowResult( string columnName, string sortType, int firstRecord)
    {
        try
        {
            GridViewHelper gridViewHelper = new GridViewHelper();
            gridViewHelper.FillDataViewToGridView(
            GetDataView(columnName, sortType, firstRecord),
            this.GridView1); ;
             
            if (GridView1.Rows.Count == 0)
            {
                ShowHide(false);
                litError.Text = "Resume not found.";
            }
            else
            {
                RedefineURL(GridView1);
                litError.Text = "";
                ShowHide(true);
            }
        }
        catch (Exception ex)
        {
            litError.Text = ex.Message;
        }
    }
     


    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        ShowResult(
        e.SortExpression, ViewState["SortDirection"].ToString(),
        Convert.ToInt32(literalCurrentPage.Text) - 1);
        if (ViewState["SortDirection"].ToString() == "ASC")
            ViewState["SortDirection"] = "DESC";
        else
            ViewState["SortDirection"] = "ASC";
    }
     
     string GetSelectItem()
     {
         string selectItem = "";
         foreach (GridViewRow gridViewRow
         in GridView1.Rows)
         {
             CheckBox checkBox = (CheckBox)
             gridViewRow.FindControl("chkResumeID");             
             if(checkBox.Checked )
             {
                selectItem +=
                      gridViewRow.Cells[10].Text + ",";
                    
             }
         }
          
         return selectItem;
     }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        try
            {
            string deleteQuery = "";
            string selectItem = GetSelectItem();         
            if (selectItem != "")
            {
                selectItem = selectItem.Substring(
                0, selectItem.Length - 1);
                deleteQuery = "delete from " +
                "tblContactByRecruiters where ResumeID in (" +
                selectItem +
                ") and  RecruiterID = " +
                Session["rcuserid"].ToString() + ";";
                RecruitVietnam rcv
                            = new RecruitVietnam();
                rcv.ConnectionString =
                connectionString;
                rcv.ExecuteSQL(deleteQuery);
                ShowResult(0);
            }
        }
        catch (Exception ex)
        {
             litError.Text = ex.Message;
        }
    }

    
    protected void btnAccept_Click(object sender, EventArgs e)
    {
        try
        {
            string updateQuery = "";
            string selectItem = GetSelectItem();
            if (selectItem != "")
            {
                selectItem = selectItem.Substring(
                0, selectItem.Length - 1);
                updateQuery = "Update tblContactByRecruiters" +
                " set ContactStatus = 2, UpdatedDate = " +
                " getdate() where ResumeID in (" +
                selectItem + ") and  RecruiterID = " +
                Session["rcuserid"].ToString() + ";";
                RecruitVietnam rcv
                            = new RecruitVietnam();
                rcv.ConnectionString =
                connectionString;
                rcv.ExecuteSQL(updateQuery);
                ShowResult(0);
            }

        }
        catch (Exception ex)
        {
            litError.Text = ex.Message;
        }
    }
    protected void btnReject_Click(object sender, EventArgs e)
    {
        try
        {
            string updateQuery = "";
            string selectItem = GetSelectItem();
            if (selectItem != "")
            {
                selectItem = selectItem.Substring(
                0, selectItem.Length - 1);
                updateQuery = "Update tblContactByRecruiters" +
                " set ContactStatus = 1, UpdatedDate = " +
                " getdate() where ResumeID in (" +
                selectItem + ") and  RecruiterID =" +
                Session["rcuserid"].ToString() + ";";
                RecruitVietnam rcv
                            = new RecruitVietnam();
                rcv.ConnectionString =
                connectionString;
                rcv.ExecuteSQL(updateQuery);
                ShowResult(0);
            }                        
        }
        catch (Exception ex)
        {
            litError.Text = ex.Message;
        }
    }
    protected void btnPending_Click(object sender, EventArgs e)
    {
        try
        {
            string updateQuery = "";
            string selectItem = GetSelectItem();
            if (selectItem != "")
            {
                selectItem = selectItem.Substring(
                0, selectItem.Length - 1);
                updateQuery = "Update tblContactByRecruiters" +
                " set ContactStatus = 3, UpdatedDate = " +
                " getdate() where ResumeID in (" +
                selectItem + ") and RecruiterID =" +
                Session["rcuserid"].ToString() + ";";
                RecruitVietnam rcv
                            = new RecruitVietnam();
                rcv.ConnectionString =
                connectionString;
                rcv.ExecuteSQL(updateQuery);
                ShowResult(0);
                 
            }

        }
        catch (Exception ex)
        {
            litError.Text = ex.Message;
        }
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        GridViewRow gridViewRow = e.Row;
        gridViewRow.Cells[10].Visible =  
        gridViewRow.Cells[11].Visible =
        gridViewRow.Cells[12].Visible = false;
    }
}


